# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=btrfs-progs
pkgver=5.7
pkgrel=0
pkgdesc="BTRFS filesystem utilities"
url="https://btrfs.wiki.kernel.org"
arch="all"
license="GPL-2.0-or-later LGPL-3.0-or-later"
depends_dev="linux-headers"
makedepends="$depends_dev acl-dev asciidoc attr-dev autoconf automake
	e2fsprogs-dev libtool lzo-dev py3-setuptools python3-dev util-linux-dev
	xmlto zlib-dev zstd-dev"
subpackages="$pkgname-doc $pkgname-static $pkgname-dev
	$pkgname-bash-completion:bashcomp:noarch
	py3-$pkgname:py3 $pkgname-libs $pkgname-extra"
source="https://www.kernel.org/pub/linux/kernel/people/kdave/btrfs-progs/btrfs-progs-v$pkgver.tar.xz"
builddir="$srcdir/$pkgname-v$pkgver"

prepare() {
	default_prepare
	./autogen.sh
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--disable-backtrace
	make
}

package() {
	make -j1 DESTDIR="$pkgdir" install install_python

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname
	install -m644 README.md CHANGES \
		"$pkgdir"/usr/share/doc/$pkgname

	mkdir "$pkgdir"/sbin
	local i; for i in btrfs btrfsck fsck.btrfs mkfs.btrfs; do
		mv "$pkgdir"/usr/bin/$i "$pkgdir"/sbin/
	done
	mv "$pkgdir"/usr/bin "$pkgdir"/usr/sbin
}

bashcomp() {
	pkgdesc="Bash completion script for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	install -D -m644 "$builddir"/btrfs-completion \
		"$subpkgdir"/usr/share/bash-completion/completions/btrfs
}

py3() {
	pkgdesc="Python 3 bindings for $pkgname"
	provides="py-btrfs-progs=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="py-btrfs-progs"  # for backward compatibility

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/python* "$subpkgdir"/usr/lib/
}

extra() {
	depends="$pkgname"
	pkgdesc="BTRFS filesystem extra utilities"

	mkdir -p "$subpkgdir"
	mv "$pkgdir"/usr "$subpkgdir"/
	rmdir "$subpkgdir"/usr/lib
}

sha512sums="72c3af13ca589f2e0b96cb7602319035ef8aab6ee224fff3544a5d0bfc013a66552dde4533ec5e64696d404b8905431cd0f25367c40fd34ea39be7c0ed8c2d16  btrfs-progs-v5.7.tar.xz"
